package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.VisitRegistrationMapper;
import com.zzyl.nursing.domain.VisitRegistration;
import com.zzyl.nursing.service.IVisitRegistrationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 来访登记信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-30
 */
@Service
public class VisitRegistrationServiceImpl extends ServiceImpl<VisitRegistrationMapper, VisitRegistration> implements IVisitRegistrationService {
    @Autowired
    private VisitRegistrationMapper visitRegistrationMapper;
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询来访登记信息
     *
     * @param id 来访登记信息主键
     * @return 来访登记信息
     */
    @Override
    public VisitRegistration selectVisitRegistrationById(Long id) {
        return getById(id);
    }

    /**
     * 查询来访登记信息列表
     *
     * @param visitRegistration 来访登记信息
     * @return 来访登记信息
     */
    @Override
    public List<VisitRegistration> selectVisitRegistrationList(VisitRegistration visitRegistration) {


        return visitRegistrationMapper.selectVisitRegistrationList(visitRegistration);


    }

    /**
     * 新增来访登记信息
     *
     * @param visitRegistration 来访登记信息
     * @return 结果
     */
    @Override
    public int insertVisitRegistration(VisitRegistration visitRegistration) {
        // 如果reservation_id为空，尝试根据其他信息查找关联的预约
        if (visitRegistration.getReservationId() == null) {
            // 根据来访人姓名、手机号、来访时间等信息查找预约
          /*  LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                    .eq(Reservation::getMobile, visitRegistration.getVisitorMobile());*/

            Reservation reservation = reservationMapper.selectOne(Wrappers.<Reservation>lambdaQuery().eq(Reservation::getMobile, visitRegistration.getVisitorMobile()));
            //reservation.getId()
            if (reservation != null) {
                visitRegistration.setReservationId(reservation.getId());
//            } else {
//                // 如果找不到关联预约，可以设置默认值或抛出异常
//                // 根据业务需求决定处理方式
//                throw new RuntimeException("未找到关联的预约信息");
            }
        }

        visitRegistration.setCreateTime(DateUtils.getNowDate());
        return save(visitRegistration)?1:0;
    }

    /**
     * 修改来访登记信息
     *
     * @param visitRegistration 来访登记信息
     * @return 结果
     */
    @Override
    public int updateVisitRegistration(VisitRegistration visitRegistration) {
        return updateById(visitRegistration) ? 1 : 0;
    }

    /**
     * 批量删除来访登记信息
     *
     * @param ids 需要删除的来访登记信息主键
     * @return 结果
     */
    @Override
    public int deleteVisitRegistrationByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除来访登记信息信息
     *
     * @param id 来访登记信息主键
     * @return 结果
     */
    @Override
    public int deleteVisitRegistrationById(Long id) {
        return removeById(id) ? 1 : 0;
    }

}
